# This class can be used as the server to collect reports from 
# the clients 

import sys
import socket
import select 
import threading
import os

BUF_SIZE = 1024

class Server:
    def __init__(self):
        self.host = ''
        self.port = 60000
        self.backlog = 5
        self.size = BUF_SIZE
        self.server = None
        self.threads = []
        pass

    def open_sock(self):
        try:
            self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP 
            self.server.bind((self.host,self.port))
            self.server.listen(self.backlog)
        except socket.error, (value,message):
            if self.server:
                self.server.close() 
                print "Could not open socket: " + message
                sys.exit(1)

    def run(self): # Override threads
        self.open_sock()
        input = [self.server, sys.stdin]  #Wait for connections as well as user input
        running = 1
        while(running):
             input_list,output_list,except_list = select.select(input,[],[])
             for item in input_list: #There are 2 items in the list
                 if(item == self.server):
                     client , addr = self.server.accept()
                     print client 
                     client_obj = Client(client,addr)
                     client_obj.start()
                     self.threads.append(client_obj)
                 elif (item == sys.stdin):
                     terminal_input = sys.std.readline()
                     if(terminal_input == constants.COORD_CLOSE_PHRASE):
                         running = 0
                         pass
                     pass
                 pass
             pass
        self.server.close()
        for item in self.threads:
            item.join()
            pass
        pass



# This class has code to communicate with the clients.
class Client(threading.Thread):
    def __init__(self,client,address):
        threading.Thread.__init__(self) # Need this for threading
        self.client = client
        self.address = address
        self.ip = address[0]
        self.port = address[1]
        self.size = BUF_SIZE
        pass
    pass
   # # Each client can request for then same file.
   # def send_list(self):
   #     file_name =  os.environ['PYTHONPATH'].split(os.pathsep) + "conf/"+ CAP_FILE_NAME
   #     f = open(file_name, "rb")
   #     data = f.read()
   #     f.close()

    # Takes the data message from the clinet
    # and sends a reply
    #def handle_data(self,data):
    #    if(data == constants.LIST_REQ):
    #        self.send_list()
    #        pass
    #    pass
    #    if(data == constants.ONE_REQ):
    #        pass
    #    pass

    def run(self):
        running = 1
        #print "IP address: ", self.ip
        #print "Port address: ", self.port
        while running:
            data = self.client.recv(self.size)
            if data:
                #self.client.send(data)
		print data
            else:
                self.client.close()
                running = 0

if __name__ == "__main__":
    s = Server()
    s.run()






